//
//  LoginView.swift
//  SinaBlog
//
//  Created by 有何不可 on 15/12/30.
//  Copyright © 2015年 有何不可. All rights reserved.
//

import UIKit

protocol LoginViewDelegate: NSObjectProtocol {
    
    func didRegisterBtn()
    
    func didLoginBtn()
}

class LoginView: UIView {
    
    weak var delegate: LoginViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setSubViews()
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setSubViews() {
        
        addSubview(animationImageView)
        addSubview(maskImageView)
        addSubview(homeImageView)
        addSubview(textLal)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
        setFrame()
    }
    
    private func setFrame() {
        
        animationImageView.translatesAutoresizingMaskIntoConstraints = false
        homeImageView.translatesAutoresizingMaskIntoConstraints = false
        textLal.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        
        //动图
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //添加蒙板
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant:150))
        
        //房子
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //文本
        addConstraint(NSLayoutConstraint(item: textLal, attribute: .Top, relatedBy: .Equal, toItem: animationImageView, attribute: .Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: textLal, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
         addConstraint(NSLayoutConstraint(item: textLal, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        //注册Btn
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Leading, relatedBy: .Equal, toItem: textLal, attribute: .Leading, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: textLal, attribute: .Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 30))
        
        //登录Btn
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Trailing, relatedBy: .Equal, toItem: textLal, attribute: .Trailing, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: textLal, attribute: .Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 30))
    }
    
    func setAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 20
        
        animation.removedOnCompletion = false
        
        animationImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    func setInit(imageName: String?, text: String) {
        
        if let OKImageName = imageName {
            
            animationImageView.hidden = true
            
            homeImageView.image = UIImage(named: OKImageName)
            
        }else {
        
            animationImageView.hidden = false
        
        }
        
        textLal.text = text
 
    }
    // MARK:--注册,登录按钮的点击方法
    
    @objc private func registerBtnClick() {
        
        print("注册")
        delegate?.didRegisterBtn()
        
    }
    
    @objc private func loginBtnClick() {
        
        print("登录")
        delegate?.didLoginBtn()
    }
    
    // MARK:--懒加载
    private lazy var animationImageView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        imgView.sizeToFit()
        
        return imgView
        
         }()
    
    private lazy var homeImageView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        imgView.sizeToFit()
        
        return imgView
        }()
    
    private lazy var textLal: UILabel = {
        
        let textLal = UILabel()
        
        textLal.textAlignment = .Center
        
        textLal.textColor = UIColor.darkGrayColor()
        
        textLal.numberOfLines = 0
        
        textLal.font = UIFont.systemFontOfSize(16)
        
        return textLal
        }()
    
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton(type: .Custom)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
        return btn
        
        }()
    
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton(type: .Custom)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("登录", forState: .Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        
        return btn
        
        }()
    
    private lazy var maskImageView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
//        imgView.backgroundColor = UIColor.redColor()
        
        return imgView
    }()

}
